package com.mallsystem.adminservice.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.mallsystem.adminservice.mapper.AdminMapper;
import com.mallsystem.common.bean.Admin;
import com.mallsystem.common.exception.AllException;
import com.mallsystem.common.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) throws AllException {
        //根据用户名查询
        Admin admin = adminMapper.selectByUsername(username);
        if(admin == null){
            throw new AllException("用户名不存在，请重新输入");
        }
        //禁用无法登录
        if(admin.getStatus() == 1){
            throw new AllException("该管理员已被禁用，请联系其他管理员");
        }
        //对用户名输入密码加密 - 使用MD5算法和盐进行加密
        String md5Pwd = SecureUtil.md5(SecureUtil.md5(password + admin.getSalt()));
        //对加密后的密码和数据库中比较
        if(!md5Pwd.equals(admin.getPassword())){
            throw new AllException("密码错误，请重新输入");
        }
        return admin;
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public boolean update(Admin admin) {
        return adminMapper.update(admin) == 1;
    }

    @Override
    public boolean chgPwd(String oldPwd, String newPwd, Integer id) throws AllException {
        /*
        判断远密码是否正确
        1.根据id查询用户信息，获取原密码和盐
        2.使用用户输入的原密码和盐加密
        3.和数据库中的原密码比较
         */
        Admin admin = adminMapper.selectById(id);
        String salt = admin.getSalt();
        //加密之后用户输入的密码
        String md5InputOldPwd = SecureUtil.md5(SecureUtil.md5(oldPwd +salt));
        if(!admin.getPassword().equals(md5InputOldPwd)){
            throw new AllException("原密码输入错误，请重新输入");
        }
        //对新密码结合盐加密
        String md5NewPwd = SecureUtil.md5(SecureUtil.md5(newPwd +salt));
        //修改密码
        admin = new Admin();
        admin.setId(id);
        admin.setPassword(md5NewPwd);
        return adminMapper.update(admin) == 1;
    }

    @Override
    public List<Admin> selectAll() {
        return adminMapper.selectAll();
    }

    @Override
    public boolean insert(Admin admin) throws AllException {
        Admin admin1 = adminMapper.selectByUsername(admin.getUsername());
        if(admin1 != null){
            throw new AllException("用户名已存在，请重新输入");
        }
        //生成盐 -- 生成随机字符串8位
        String salt = RandomUtil.randomString(8);
        String password = SecureUtil.md5(SecureUtil.md5("123" + salt));
        admin.setSalt(salt);
        admin.setPassword(password);
        admin.setStatus(0);
        return adminMapper.insert(admin) == 1;
    }

    @Override
    public boolean updatePassword(Admin admin) throws AllException {
        String salt = adminMapper.selectById(admin.getId()).getSalt();
        if (adminMapper.selectById(admin.getId()) == null) {
            throw new AllException("管理员不存在");
        }
        //重置密码
        String password = SecureUtil.md5(SecureUtil.md5("123" + salt));
        admin.setPassword(password);
        return adminMapper.update(admin) == 1;
    }

    @Override
    public boolean updateStatus(Admin admin) throws AllException {
        Admin admin1 = adminMapper.selectById(admin.getId());
        if(admin1 == null){
            throw new AllException("管理员不存在");
        }
        Integer status = admin1.getStatus() == 1 ? 0 : 1;
        admin1.setStatus(status);
        return adminMapper.update(admin1) == 1;
    }

    @Override
    public List<Map<String, Integer>> selectCategoryTop5() {
        return adminMapper.selectCategoryTop5();
    }

    @Override
    public List<Map<String, Integer>> selectOrderByDay() {
        return adminMapper.selectOrderByDay();
    }

}
